﻿using BCCommon;
using BCEntity.Common.Banner;
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;

namespace BCData.Common.Banner
{
    /// <summary>
    /// Banner类别数据处理
    /// </summary>
    public class BannerTagcodeData : IBannerTagcodeData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY_EXISTS = @"SELECT 1 FROM bannertagcode";
        private const string QUERY = @"SELECT BannerTagcode,Description,IsSystem,Enabled FROM BannerTagcode";
        public BannerTagcodeData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }

        public BannerTagcodeEntity Add(BannerTagcodeEntity bannerTagcodeEntity)
        {
            string sql = @"INSERT INTO BannerTagcode(BannerTagcode,Description)VALUES(?BannerTagcode,?Description);
                            {0} WHERE BannerTagcode=?BannerTagcode;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BannerTagcode", bannerTagcodeEntity.BannerTagcode, DbType.AnsiString, size: 50);
            parameters.Add("?Description", bannerTagcodeEntity.Description, DbType.AnsiString, size: 100);
            return this.databaseContext.Master.QueryFirstOrDefault<BannerTagcodeEntity>(string.Format(sql, QUERY), parameters, databaseContext.Transaction);
        }

        public bool CheckBannerTagcodeExists(string bannerTagcode)
        {
            string sql = @"SELECT 1 FROM BannerTagcode WHERE BannerTagcode=?BannerTagcode LIMIT 1;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BannerTagcode", bannerTagcode, DbType.AnsiString, size: 50);
            object result = this.databaseContext.Slave.ExecuteScalar(sql, parameters);
            return result != null;
        }

        public void Delete(string bannerTagcode)
        {
            string sql = @"DELETE FROM BannerTagcode WHERE BannerTagcode=?BannerTagcode AND IsSystem=0;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BannerTagcode", bannerTagcode, DbType.AnsiString, size: 50);
            this.databaseContext.Master.Execute(sql, parameters, databaseContext.Transaction);
        }

        public BannerTagcodeEntity Get(string bannerTagcode)
        {
            string sql = string.Format("{0} WHERE BannerTagcode=?BannerTagcode;", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BannerTagcode", bannerTagcode, DbType.AnsiString, size: 50);
            return this.databaseContext.Slave.QueryFirstOrDefault<BannerTagcodeEntity>(sql, parameters);
        }

        public IEnumerable<BannerTagcodeEntity> GetList()
        {
            string sql = string.Format("{0} WHERE Enabled=1;", QUERY);
            return this.databaseContext.Slave.Query<BannerTagcodeEntity>(sql);
        }

        public IEnumerable<BannerTagcodeEntity> GetPageableList(string tagCode, out int dataCount, bool? ascending, int pageIndex = 1, int pageSize = 20)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (!string.IsNullOrWhiteSpace(tagCode))
            {
                wheres.Add("BannerTagcode=?BannerTagcode");
                parameters.Add("?BannerTagcode", tagCode, DbType.AnsiString, size: 50);
            }
            string sql = string.Format(@"{0} {1} ORDER BY BannerTagcode {4} LIMIT {2},{3}; 
                               SELECT Count(1) FROM BannerTagcode",
               QUERY,
               wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
               (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
               pageSize,
               ascending.HasValue && ascending.Value ? "ASC" : "DESC");

            var result = databaseContext.Slave.QueryMultiple<BannerTagcodeEntity, int>(sql, parameters);

            dataCount = result.Item2;

            return result.Item1;
        }

        #region 检查Banner类别是否存在
        public async Task<bool> Exists(string bannerTagcode)
        {
            string sql = string.Format(@"{0} WHERE BannerTagcode=?BannerTagcode", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BannerTagcode", bannerTagcode, DbType.AnsiString);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }
        #endregion

        public BannerTagcodeEntity Update(BannerTagcodeEntity bannerTagcodeEntity)
        {
            string sql = @"UPDATE BannerTagcode SET
                                Description=?Description
                           WHERE BannerTagcode=?BannerTagcode AND IsSystem=0;
                            {0} WHERE BannerTagcode=?BannerTagcode;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BannerTagcode", bannerTagcodeEntity.BannerTagcode, DbType.AnsiString, size: 50);
            parameters.Add("?Description", bannerTagcodeEntity.Description, DbType.AnsiString, size: 100);
            return this.databaseContext.Master.QueryFirstOrDefault<BannerTagcodeEntity>(string.Format(sql, QUERY),  parameters, databaseContext.Transaction);
        }


        #region 启用Banner类别
        public async Task<bool> Enabled(string bannerTagcode)
        {
            string sql = @"UPDATE bannertagcode SET Enabled=1 WHERE BannerTagcode=?BannerTagcode";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BannerTagcode", bannerTagcode, DbType.AnsiString);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion

        #region 禁用Banner类别
        public async Task<bool> Disabled(string bannerTagcode)
        {
            string sql = @"UPDATE bannertagcode SET Enabled=0 WHERE BannerTagcode=?BannerTagcode";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BannerTagcode", bannerTagcode, DbType.AnsiString);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion
    }
}
